package com.example.demo.mq.comsumer;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.User;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import javax.jms.TextMessage;

@Component
public class ActiveMqListener {

    @JmsListener(destination = "${activeMq.topicName}")
//    @SendTo("topic_test")
    public void ACustomerReceiveTopic(String message) {
        System.out.println("监听topic=============A消费者");
        User user = JSONObject.parseObject(message, User.class);
        System.out.println("消息：" + message);
        System.out.println("学生姓名：" + user.getUsername());
    }



//    @JmsListener(destination = "${activeMq.topicName}")
////    @SendTo("topic_test")
//    public void BCustomerReceiveTopic(String message) {
//        System.out.println("监听topic=============B消费者");
//        User user = JSONObject.parseObject(message, User.class);
//        System.out.println("消息：" + message);
//        System.out.println("学生姓名：" + user.getUsername());
//    }

    @JmsListener(destination = "${activeMq.queueName}")
    public void receiveQueue(Message message) {
        System.out.println("监听queue========================");
        System.out.println("消息：" + message.getPayload());
        if (message instanceof TextMessage){
            TextMessage textMessage = (TextMessage) message;
            try{
                System.out.println("接收的消息：{}" + textMessage.getText());
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @JmsListener(destination = "queue_delay")
    public void receiveDelayQueue(Message message) {
        System.out.println("监听queue========================");
        System.out.println("消息：" + message.getPayload());
        if (message instanceof TextMessage){
            TextMessage textMessage = (TextMessage) message;
            try{
                System.out.println("接收的消息：{}" + textMessage.getText());
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }


}